package asynq

import (
	"context"
	"github.com/hibiken/asynq"
	"learn/pkg/logger"
)

func loggingMiddleware(h asynq.Handler) asynq.Handler {
	return asynq.HandlerFunc(func(ctx context.Context, t *asynq.Task) error {
		//start := time.Now()
		//time.Since(start)
		err := h.ProcessTask(ctx, t)
		if err != nil {
			logger.InfoJSON("asynq work err", t.Type(), string(t.Payload())+": err = "+err.Error())
			return err
		} else {
			logger.InfoString("asynq work", t.Type(), string(t.Payload()))
		}
		return nil
	})
}
